package com.allen.control;

import com.allen.bean.User;
import com.allen.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static com.sun.javafx.fxml.expression.Expression.add;

@WebServlet("/userCtrl")
public class UserControl extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String parm = req.getParameter("parm");
        switch (parm) {
            case "login":
                login(req, resp);
                break;
            case "pwd":
                alterPwd(req, resp);
                break;
            case "list":
                list(req, resp);
                break;
            case "add":
                addUser(req, resp);
                break;
            case "toEdit":
                toEdit(req, resp);
                break;
            case "edit":
                edit(req, resp);
                break;
            case "delete":
                delete(req, resp);
                break;
            case "check":
                check(req,resp);
        }
    }

    private void check(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("username");
        int i = new UserService().valid(name);
        resp.getWriter().print(i);
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = new UserService().login(username, password);
        if (user != null) {
            req.getSession().setAttribute("user", user);
            int state = user.getState();
            int deleted = user.getDeleted();
            if (state == 1) {
                resp.getWriter().print(1);
            } else if (deleted == 1) {
                resp.getWriter().print(2);
            } else {
            resp.getWriter().print(3);
            }
        } else {
            resp.getWriter().print(4);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer id = Integer.parseInt(req.getParameter("id"));
        new UserService().deleteUser(id);
        resp.sendRedirect("/erp/userCtrl?parm=list");
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        new UserService().updateUser(req, resp);
        resp.sendRedirect("/erp/userCtrl?parm=list");
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.valueOf(req.getParameter("id"));
        User user = new UserService().findById(id);
        req.setAttribute("user", user);
        req.getRequestDispatcher("/user/edit.jsp").forward(req, resp);

    }

    private void addUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        new UserService().addUser(req, resp);
        resp.sendRedirect("/erp/userCtrl?parm=list");
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<User> users = new UserService().getAllUser();
        req.setAttribute("users", users);
        req.getRequestDispatcher("/user/list.jsp").forward(req, resp);
    }

    private void alterPwd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pwd = req.getParameter("pwd");
        Integer id = Integer.valueOf(req.getParameter("id"));
        int i = new UserService().alterPwd(pwd, id);
        if (i > 0) {
            resp.setContentType("text/html; charset=utf-8");
            resp.getWriter().print("修改成功");
        }
    }


}

